package ModelLayer;
import java.util.Iterator;
import java.util.ArrayList;
/**
 * Write a description of class DvdContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DvdContainer
{
    private static DvdContainer instance;
    private ArrayList<Dvd> listOfDvds;

    private DvdContainer()
    {
        listOfDvds = new ArrayList();
    }

    public static DvdContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DvdContainer();
        }
        return instance;
    }

    private void addDvd(Dvd name)
    {
        listOfDvds.add(name);
    }

    public void removeDvd(String title)
    {
        Iterator<Dvd> it = listOfDvds.iterator();
        while(it.hasNext())
        {
            Dvd t = it.next();
            String titleOfDvd = t.getTitle();
            if(titleOfDvd.equals(title))
            {
                it.remove();
            }
        }
    }

    public Dvd findDvd(String title)
    {
        int index = 0;
        boolean found = false;
        while(index < listOfDvds.size() && !found)
        {
            if(listOfDvds.get(index).getTitle().equals(title))

            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (found ==true)
        {
            return listOfDvds.get(index);
        }
        else 
        {
            return null;
        }
    }

    public void createDvd(String barcode, String title, String artist, String publicationDate)
    {
        if(!search(title))
        {
            Dvd d = new Dvd(barcode, title, artist, publicationDate);
            listOfDvds.add(d);
        }
    }

    public int createCopy(String title,String serialNumber, String purchaseDate, String purchasePrice)
    {
        if(findDvd(title)!=null)
        {
            findDvd(title).createCopy(serialNumber, purchaseDate, purchasePrice);
            return 1;
        }
        else
        {
            return 0;
        }
    }

    public void removeCopy(String serialNumber, String title)
    {
        findDvd(title).removeCopy(serialNumber);
    }

    public Copy findCopy(String serialNumber, String title)
    {
        return findDvd(title).findCopy(serialNumber);
    }

    public ArrayList<Dvd> getList()
    {
        return listOfDvds;
    }
    
    private boolean search(String title)
    {    
        for (Dvd dvd : listOfDvds)
        {
            if (dvd.getTitle().equals(title)) 
            {
                return true;
            }
        }
        return false; 
    }
}
